package com.yhn.controller;

import com.hospital.common.base.Result;
import com.hospital.common.dto.section.SectionStatsDto;
import com.yhn.service.SectionStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/sections/stats")
//@CrossOrigin(origins = "*", allowedHeaders = "*")
public class SectionStatsController {
    @Autowired
    private SectionStatsService sectionStatsService;

    /**
     * 获取单个板块统计信息
     */
    @GetMapping("/{sectionId}")
    public Result<SectionStatsDto> getSectionStats(@PathVariable("sectionId") Long sectionId) {
        SectionStatsDto stats = sectionStatsService.getSectionStats(sectionId);
        if (stats == null) {
            return Result.fail(40401, "板块不存在");
        }
        return Result.success(stats);
    }

    /**
     * 获取所有板块统计信息
     */
    @GetMapping("/all")
    public Result<List<SectionStatsDto>> getAllSectionsStats() {
        List<SectionStatsDto> statsList = sectionStatsService.getAllSectionsStats();
        return Result.success(statsList);
    }

    /**
     * 计算单个板块统计（用于测试）
     */
    @PostMapping("/calculate/{sectionId}")
    public Result<SectionStatsDto> calculateSectionStats(@PathVariable("sectionId") Long sectionId) {
        try {
            SectionStatsDto stats = sectionStatsService.calculateSectionStats(sectionId);
            if (stats == null) {
                return Result.fail(40401, "板块不存在");
            }
            return Result.success(stats);
        } catch (Exception e) {
            return Result.fail(50001, "统计计算失败: " + e.getMessage());
        }
    }

    /**
     * 批量计算所有板块统计
     */
    @PostMapping("/calculate/all")
    public Result<String> calculateAllSectionsStats() {
        sectionStatsService.calculateAllSectionsStats();
        return Result.success("批量统计任务已触发");
    }
}
